/**
 * Created with JetBrains PhpStorm.
 * User: jiangshuguang
 * Date: 14-10-27
 * Time: 下午12:58
 * To change this template use File | Settings | File Templates.
 */

(function(){
    var dialog = require("iphone:widget/ui/dialog/dialog.js").dialog({width:400}),
        util = require("iphone:widget/ui/libs/util.js").util,
        DConfirm = require("iphone:widget/ui/dconfirm/dconfirm.js").DConfirm,
        $page = $(".page-recharge"),
        $bankList = $page.find(".bank-select"),
        $bankContainer = $bankList.find(".bank-container"),
        $rechargeAmount = $page.find(".recharge-amount"),
        $rechargePreview = $page.find('.recharge-preview'),
        $btnRecharge = $page.find(".btn-recharge");

    P.createPage({
        _data:{
            isIdVerify:false
        },

        _create:function(){

            var idVerify = __inline("./idverify.tmpl");

            var idVerifyConfirm,
                html = idVerify();
            var userFund = P.data('user_fund'),
                activity = P.data('activity'),
                activityType = userFund['activity_type']?userFund['activity_type']:activity['activity_type'],
                resAmount = userFund['activity_type']?userFund['res_amount']:activity['amount'];

            idVerifyConfirm = new DConfirm({
                title:'实名验证',
                width:500,
                content:html
            });
            this.idVerify();
            this.data({
                'idVerifyConfirm':idVerifyConfirm,
                'activityType':activityType,
                'resAmount':resAmount
            });
        },


        //获取实名认证的状态
        idVerify:function(){
            var me = this;
            $.get("/account/getuserstatus",function(ret,status){
                 if(status == "success" && ret.error_no==0 && ret.data.id_card){
                      me.data('isIdVerify',true);
                 }
            },"json");
        },

        _init:function(){
            var me = this,
                $idVerify = $("#idverify"),
                idVerifyConfirm = this.data("idVerifyConfirm");

            //实名认证事件
            idVerifyConfirm.on("confirm",function(callback){
                var  id_card = $('#id_card').val(),
                     re_id_card = $('#re_id_card').val(),
                     id_card_name = $('#id_card_name').val();
                $idVerify.find(".tip").text("");
                if(!id_card_name) {
                    $idVerify.find(".tip").text('请输入姓名。');
                    return;
                }
                if(!id_card) {
                    $idVerify.find(".tip").text('请输入身份证号码。');
                    return;
                }
                if(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(id_card) == false) {
                    $idVerify.find(".tip").text('请输入正确的身份证号');
                    return;
                }
                if(!re_id_card) {
                    $idVerify.find(".tip").text('请再次确认身份证号。');
                    return;
                }
                if(re_id_card != id_card) {
                    $idVerify.find(".tip").text('2次输入的身份证号不一致，请确认。');
                    return;
                }
                $.post("/account/setidcard",{
                    id_card_name:id_card_name,
                    id_card:id_card
                },function(ret,status){
                    if(ret.error_no == 0){
                        $idVerify.find(".tip").text('实名认证成功');
                        me.data("isIdVerify",true);
                        setTimeout(function(){
                            callback();
                        },1000);
                    }else{
                        $idVerify.find(".tip").text(ret.error_message);
                    }
                },"json");
            });

            //加载更多银行
            $bankList.find(".btn-more").click(function(){
                $bankList.find(".more-bank").slideDown();
            });

            //购买提交
            $btnRecharge.click(function(){
                var amount = $rechargeAmount.val();
                if(!!!amount || !/^\d+$/.test(amount)){
                    dialog.show("购买金额输入错误");
                    return false;
                }
                if(amount>me.data('resAmount')){
                    dialog.show("您只需要支付￥"+me.data('resAmount'));
                    return false;
                }
                if(!$bankContainer.find(".active").length){
                    dialog.show("请选择银行");
                    return false;
                }

                if(!$("#agreement").is(':checked')) {
                    dialog.show("请确认阅读果6宝服务协议");
                    return;
                }

                if(!me.data("isIdVerify")){
                    idVerifyConfirm.show();
                    return false;
                }

                var gateId = $bankContainer.find(".active").attr("data-id").substr(1);
                window.open('' +
                    '/pay/createiphone6payserial?' +
                    'amount=' + amount +
                    '&pid=4'+
                    '&gate_id=' + gateId+
                    '&activity_type='+me.data('activityType'));

                dialog.show('<div style="text-align: left;font-size:14px;">请在新的窗口中登录网上银行进行付款。在付款完成后可以在真融宝官网查看我的交易记录查看充值结果，如果有问题可以稍后进行查看；如果长时间未收到本次充值的金额，请通过网站下方联系方式联系我们</div>');
                return true;
            });

            //加载更多银行
            $bankContainer.find("a").click(function(){
                if(!$(this).hasClass("btn-more")){
                    $bankList.find("a").removeClass("active");
                    $(this).addClass("active");
                }
            });

            //收益预览关闭
            $rechargePreview.find(".close").click(function(){
                $rechargePreview.slideUp();
            });

            $page.find('.btn-profit').click(function(){
                me.profitPreview();
            })
        },

        profitPreview:function(){
            var days = P.data('days'),
                html = "",
                yearProfitMap = {
                    1:'1586.40',
                    2:'1826.40',
                    3:'2066.40',
                    4:'2336.40'
                },
                dayProfitMap = {
                    1:'440.67',
                    2:'507.34',
                    3:'574.00',
                    4:'649.00'
                },
                type = this.data('activityType');

            $.each(days,function(k,v){
                if(k!=days.length-1){
                    html+='' +
                        '<tr>' +
                        '<td>'+v+'</td>' +
                        '<td>￥'+dayProfitMap[type]+'</td>' +
                        '</tr>'
                }else{
                    html+='' +
                        '<tr>' +
                        '<td>'+v+'</td>' +
                        '<td>￥'+yearProfitMap[type]+'</td>' +
                        '</tr>'
                }
            });

            $rechargePreview.find("tbody").html(html);
            $rechargePreview.slideDown();
        }
    },"recharge");
})();
